#include"SimpleDateClassRealization.h"

// 全缺省的构造函数
Date::Date(int year, int month, int day)
	:_year(year)
	,_month(month)
	,_day(day)
{
	if (!CheckDate())
	{
		cout << "日期非法" << endl;
		Print();
	}
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}
// 日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		if (_month == 12)
		{
			++_year;
			_month = 1;
		}
		else
		{
			++_month;
		}
	}
	return *this;
}
// 日期+天数
Date Date::operator+(int day) const
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}
// 日期-天数
Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day < 1)
	{
		if (_month == 1)
		{
			--_year;
			_month = 12;
		}
		else
		{
			--_month;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
// 前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
// 后置++
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}
// 后置--
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}
// 前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
// >运算符重载
bool Date::operator>(const Date& d) const
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			return _day > d._day;
		}
	}
	return false;
}
// ==运算符重载
bool Date::operator==(const Date& d) const
{
	return _year == d._year && _month == d._month && _day == d._day;
}
// >=运算符重载
bool Date::operator >= (const Date& d) const
{
	return *this > d || *this == d;
}
// <运算符重载
bool Date::operator < (const Date& d) const
{
	return d > *this;
}
// <=运算符重载
bool Date::operator <= (const Date& d) const
{
	return d >= *this;
}
// !=运算符重载
bool Date::operator != (const Date& d) const
{
	return !(*this == d);
}
// 日期-日期 返回天数
int Date::operator-(const Date& d) const
{
	// solution 1
	int n = 0;
	int flag = 1;
	Date max = *this, min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	while (min != max)
	{
		++min;
		++n;
	}
	return flag * n;
}

// 日期类流插入、流提取
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日";
	return out;
}
istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请输入日期:>";
		in >> d._year >> d._month >> d._day;
		bool flag = d.CheckDate();
		if (flag)
		{
			break;
		}
		else
		{
			cout << "日期非法：";
			d.Print();
			cout << "请重新输入！" << endl;
		}
	}
	return in;
}